package Servlet;

/**
 * Created by 2015302580057 on 2017/7/12.
 * 为了精简代码量，找回密码的servlet也使用这一个了
 */

import dao.UserDao;
import daoImpl.UserDaoImpl;
import entity.User;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(name = "Register", urlPatterns = {"/Register"})
public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取前端传送来的值
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("myemail");
        String checknum = request.getParameter("checknum");
        String realchecknum = (String) request.getSession().getAttribute("checknum");

        response.setContentType("text/json;charset=utf-8");
        //初始化数据库
        UserDao dao = new UserDaoImpl();

        PrintWriter pw = response.getWriter();
        JSONObject object = new JSONObject();//定义一个json对象，用来存储执行返回结果

        String action = request.getParameter("action");//判断要进行的操作
        //注册请求
        if (action.equals("register")) {
            User u1 = dao.queryUserByEmail(email);
            User u2 = dao.queryUserByUserName(username);

            if ((u1 != null) || (u2 != null)) {
                object.put("code", 1);//用户已经存在，拒绝注册操作
                pw.print(object);
            } else if (!checknum.equals(realchecknum)){
                object.put("code", 2);//验证码错误
                pw.print(object);
            } else{
                User newUser = new User(username, password, email);
                int row = dao.addUser(newUser);
                if (row == 1) {
                    object.put("code", 0);//创建新用户成功
                    pw.print(object);
                } else {
                    object.put("code", 3);//创建新用户失败
                    pw.print(object);
                }
            }
        }

        //修改密码操作
        if (action.equals("changepassword")){
            User u = dao.queryUserByEmail(email);
            if (u==null){
                object.put("code", 1);//用户不存在
                pw.print(object);
            }else if (!checknum.equals(realchecknum)){
                object.put("code", 2);//验证码错误
                pw.print(object);
            }else {
                u.setPassword(password);
                dao.updateUserPassword(u);
                object.put("code", 0);//密码修改成功
                pw.print(object);
            }

        }


//        User u1 = dao.queryUserByEmail(email);
//        User u2 = dao.queryUserByUserName(username);
//        PrintWriter pw = response.getWriter();
//        JSONObject object = new JSONObject();//定义一个json对象，用来存储执行返回结果，0代表成功，1代表用户已存在，2代表添加失败
//        if (checknum.equals(realchecknum)){
//            if (u2!=null){//用户已经存在
//                object.put("code",1);//用户已经存在，拒绝注册操作
//                pw.print(object);
//            }else if (u1 != null) { // email已注册，用户已存在
//                u1.setPassword(password);
//                dao.updateUserPassword(u1);
//                object.put("code",2);//对已存在用户的密码修改成功
//                pw.print(object);
//            }else {
//                User newUser = new User(username,password,email);
//                int row = dao.addUser(newUser);
//                if (row==1){
//                    object.put("code",0);//创建新用户成功
//                    pw.print(object);
//                }else {
//                    object.put("code",3);//创建新用户失败
//                    pw.print(object);
//                }
//            }
//        }
//        else {
//            object.put("code",4);//验证码输入错误
//            pw.print(object);
//        }
//
        pw.close();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }
}
